package com.huarun.common.util;

import com.alibaba.fastjson.JSONObject;
import java.io.PrintStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

public class Md5SignUtil
{
  public static String sort(Map<String, Object> params, boolean skipBlank)
  {
    List<Map.Entry<String,Object>> list = new ArrayList(params.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Object>>()
    {
      public int compare(Map.Entry<String, Object> mapping1, Map.Entry<String, Object> mapping2) {
        return ((String)mapping1.getKey()).compareTo((String)mapping2.getKey());
      }
    });
    StringBuffer sb = new StringBuffer();
    boolean isFirst = true;
    for (Map.Entry mapping : list)
      if ((!skipBlank) || (!StringUtils.isBlank(mapping.getValue().toString())))
      {
        if (!isFirst)
          sb.append("&");
        sb.append((String)mapping.getKey());
        sb.append("=");
        sb.append(mapping.getValue());

        isFirst = false;
      }
    return sb.toString();
  }

  public static String sort(Map<String, Object> params)
  {
    return sort(params, true);
  }

  public static String genHMAC(String params, String key)
  {
    byte[] result = null;
    try
    {
      SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");

      Mac mac = Mac.getInstance("HmacSHA1");

      mac.init(signinKey);

      byte[] rawHmac = mac.doFinal(params.getBytes());
      result = Base64.encodeBase64(rawHmac);
    }
    catch (NoSuchAlgorithmException e) {
      System.err.println(e.getMessage());
    } catch (InvalidKeyException e) {
      System.err.println(e.getMessage());
    }
    if (null != result) {
      return new String(result);
    }
    return null;
  }

  public static void main(String[] args)
  {
    Map params = new HashMap();
    params.put("Action", "ListDevices");
    params.put("PageSize", Integer.valueOf(20));
    params.put("PageNum", Integer.valueOf(0));
    params.put("ProjectID", Integer.valueOf(102));
    params.put("Region", "shenzhen");
    params.put("Timestamp", Integer.valueOf(1548382222));
    DeviceTypeClazz dt = new DeviceTypeClazz(Integer.valueOf(20));
    params.put("Filter", JSONObject.toJSONString(dt));
    params.put("Filter", "{\"DeviceType\":[\"20\"]}");
    params.put("Nonce", "abc");
    params.put("SecretId", "5c496e27f73c970e8e19e07d");
    params.put("Version", "1.0");

    System.out.println(sort(params));
    System.out.println(genHMAC(sort(params), "kYTMVliS4GaXLmU9FrXC8lOWR5JYiVnU"));
  }
}

/* Location:           D:\msg\反编译\Webuilding-0.0.1-SNAPSHOT(5)\BOOT-INF\classes\
 * Qualified Name:     com.huarun.common.util.Md5SignUtil
 * JD-Core Version:    0.6.2
 */